﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace PublicFunction
{
    /// <summary>
    /// 创建一个自定义XML文件操作类
    /// </summary>
    public class MyXMLHelper
    {
        #region 数据结构
        /// <summary>
        /// 文件是否打开成功（包括根节点是否存在）
        /// </summary> 
        //public bool IsOpened
        //{
        //    get { return openSuccess; }
        //}

        /// <summary>
        /// 文件路径
        /// </summary>
        //public string XMLFilePath { get { return xmlFilePath; } }
        #endregion

        /// <summary>
        /// 默认构造函数
        /// </summary>
        //public MyXMLHelper()
        //{
        //    xmlDoc = new XmlDocument();
        //}

        /// <summary>
        /// 
        /// </summary>
        //~MyXMLHelper()
        //{
        //    xmlDoc = null;
        //    xmlRoot = null;
        //}

        /// <summary>
        /// 保存数据到xml文件
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="path">路径</param>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        //public static FileOptErr SaveDataToXML<T>(string path, T obj)
        //{
        //    if (File.Exists(path) == false)
        //    {
        //        var f = File.Create(path);
        //        f.Close();

        //        if (File.Exists(path) == false)
        //            return FileOptErr.CreateFileErr;
        //    }

        //    try
        //    {
        //        if(SerializeToXml<T>(path, obj))
        //            return FileOptErr.OK;
        //        else
        //            return FileOptErr.SaveErr;
        //    }
        //    catch (Exception ex)
        //    {
        //        string err = ex.Message;
        //        return FileOptErr.SaveErr;
        //    }
        //}

        #region 序列化和反序列化功能
        /// <summary>
        /// 将数据配置信息序列化到XML文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <param name="obj"></param>
        //private static bool SerializeToXml<T>(string filePath, T obj)
        //{
        //    try
        //    {
        //        using (StreamWriter writer = new StreamWriter(filePath))
        //        {
        //            XmlSerializer xs = new XmlSerializer(typeof(T));
        //            xs.Serialize(writer, obj);
        //        }
        //        return true;
        //    }
        //    catch (Exception ex) {
        //        string err = ex.Message;
        //        return false;
        //    }
        //}

        /// <summary>
        /// 从XML文件反序列化数据配置信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static T DeserializeFromXml<T>(string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                    throw new ArgumentNullException(filePath + " not Exists");
                using StreamReader reader = new(filePath);
                XmlSerializer xs = new(typeof(T));
                T ret = (T)xs.Deserialize(reader);
                return ret;
            }
            catch (Exception ex)
            {
                MyLogFunction.SaveSysEventLog("XML反序列失败", ex.Message);
                return default;
            }
        }
        #endregion


        /// <summary>
        /// 
        /// </summary>
        //protected XmlDocument xmlDoc = null;
        /// <summary>
        /// 
        /// </summary>
        //protected XmlNode xmlRoot = null;
        /// <summary>
        /// 
        /// </summary>
        //protected string xmlFilePath = string.Empty;//配置表路径      
        //private bool openSuccess = false;
    }
}
